package dao

import (
	"github.com/astaxie/beego/orm"
	"xiaoercaidan/publicResources/models"
	"errors"
	"github.com/astaxie/beego/validation"
	"log"
)

func InserCategory(category *models.Category) (error) {

	o := orm.NewOrm()

	_, err := o.Insert(category)

	return err

}

func QeryCategory(params map[string]string) (*[]models.Category) {

	o := orm.NewOrm()

	var category []models.Category

	var qs orm.QuerySeter

	qs = o.QueryTable("category")

	for k, v := range params {
		qs = qs.Filter(k + "__exact", v)
	}

	qs.All(&category)

	print(category)

	if (len(category) > 0) {
		return &category
	} else {
		return nil
	}

}

func InserGoods(goods *models.Goods) (error) {

	o := orm.NewOrm()
	_, err := o.Insert(goods)

	if err == nil {
		return nil
	} else {
		return err
	}

}

func QeryGoods(params map[string]string) (*[]models.Goods) {

	o := orm.NewOrm()

	var goods []models.Goods

	var qs orm.QuerySeter

	qs = o.QueryTable("goods")

	for k, v := range params {

		if (k == "name") {
			qs = qs.Filter(k + "__icontains", v)
		} else {
			qs = qs.Filter(k + "__exact", v)
		}

	}

	qs.All(&goods)

	if (len(goods) > 0) {
		return &goods
	} else {
		return nil
	}

}

func QeryGoodsById(goodsId string) (models.Goods) {

	o := orm.NewOrm()

	var goods models.Goods

	var qs orm.QuerySeter

	qs = o.QueryTable("goods")

	qs = qs.Filter("id__exact", goodsId)

	qs.One(&goods)

	return goods

}

func DeleteCategory(ids string) (error) {

	o := orm.NewOrm()

	_, err := o.Raw("delete from category where id in(" + ids + ")").Exec()

	return err
}

func DeleteGoods(shopId, ids string) (error) {

	o := orm.NewOrm()
	o.Begin()
	sql := "delete from goods where shop_id = " + shopId + " and  id in(" + ids + ")"
	sqlDelImg := "delete from goods_image where good_id in(" + ids + ")"

	_, err := o.Raw(sql).Exec()
	if (err != nil) {
		return err
	} else {
		_, errDelImg := o.Raw(sqlDelImg).Exec()
		if (errDelImg == nil) {
			o.Commit()
		}
		return errDelImg
	}
}

func checkGoods(u *models.Goods) (err error) {
	valid := validation.Validation{}
	b, _ := valid.Valid(&u)
	if !b {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
			return errors.New(err.Message)
		}
	}
	return nil
}

func UpdateCategory(cate *models.Category) (error) {

	o := orm.NewOrm()

	_, err := o.QueryTable("category").Filter("id", cate.Id).Update(orm.Params{
		"name": cate.Name,
		"shop_id": cate.ShopId,
		"parent_id": cate.ParentId,
	})

	return err

}

func UpdateGoods(goods *models.Goods) (error) {

	if err := checkGoods(goods); err != nil {
		return err
	}

	o := orm.NewOrm()
	o.Begin()
	_, err := o.QueryTable("goods").Filter("id", goods.Id).Update(orm.Params{
		"name": goods.Name,
		"shop_id": goods.ShopId,
		"original_price": goods.OriginalPrice,
		"stock": goods.Stock,
		"category_id": goods.CategoryId,
		"price": goods.Price,
	})
	if (err != nil) {
		return err
	} else {
		var er error
		for _, value := range goods.ImageList {
			_, ere := o.QueryTable("goods_image").Filter("id", value.Id).Update(orm.Params{
				"path": value.Path,
				"type": value.Type,
				"good_id": value.GoodId,
			})
			er = ere
		}

		if (er == nil) {
			o.Commit()
		}
		return er
	}

}
